<?php


namespace app\admin\traits;

/**
 * Class HtnsTrait
 * @package app\admin\traits
 * @desc 海淘女神系统相关服务
 */
trait HtnsTrait
{
    /**
     * @desc curl get 请求
     * @param string $url 地址
     * @param int $type
     * @return bool|int|string
     */
    public function httpCurlGet($url, $type = 1 , $header = [] , $method = '' , $data = [])
    {
        $curl = curl_init();
        if($header){
            curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        }
        if($method){
            curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        }
        curl_setopt($curl, CURLOPT_TIMEOUT, 5000);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
        curl_setopt($curl, CURLOPT_URL, $url);
        if ($type == 1) {
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        }
        if($method == 'PUT'){
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);//post的数据
        }
        $res = curl_exec($curl);
        if ($res) {
            curl_close($curl);
            return $res;
        } else {
            $error = curl_errno($curl);
            curl_close($curl);
            return $error;
        }
    }

    /**
     * @desc curl post请求
     * @param string $url 请求地址
     * @param array $data 请求数据
     * @param array $header 头文件
     * @param string $method 请求方法,默认:POST,PUT
     * @return bool|string
     */
    public function httpCurlPost($url, $data = [], $header = [] , $method = '')
    {
        $ch = curl_init();//初始化curl
        curl_setopt($ch, CURLOPT_URL, $url);//抓取指定网页
        //如果是请求的是https页面，需要添加下面两个参数，规避ssl的证书检查
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // https请求 不验证证书和hosts
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        //header头信息的添加--数组
//        curl_setopt($ch, CURLOPT_HTTPHEADER, array("Connection: keep-alive",  "Content-Type: multipart/form-data", "Authorization:TOKEN " . $token));
        if($header){
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        }
        if($method){
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
            curl_setopt($ch, CURLOPT_PUT, 1);
        }else{
            curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
        }

        curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
//        curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);//上传文件或者以上传文件的形式post数据
//        curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
        curl_setopt($ch, CURLOPT_TIMEOUT, 300); // 设置超时限制防止死循环
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//post的数据
        $datav     = curl_exec($ch);//得到返回的数据
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);//得到的响应码
        http_response_code($httpCode);//设置响应状态码
        if (curl_errno($ch)) {
            echo 'Errno' . curl_error($ch);//捕抓异常
        }
        curl_close($ch);
        return $datav;
    }

    /**
     * desc 统一返回格式数据
     * @param int $code 状态码 0:失败,1:成功
     * @param string $msg 提示信息
     * @param array $data 数据体
     * @return array
     */
    public function returnArr($code, $msg = '', $data = [])
    {
        return compact('code', 'msg', 'data');
    }

    public function curlPut($url, $data , $header = [])
    {
//        $data = json_encode($data);
        $ch   = curl_init(); //初始化CURL句柄
        if($header){
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        }
        curl_setopt($ch, CURLOPT_URL, $url); //设置请求的URL
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //设为TRUE把curl_exec()结果转化为字串，而不是直接输出
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); //设置请求方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//设置提交的字符串
        $output = curl_exec($ch);
        curl_close($ch);
        return json_decode($output, true);
    }
}